

/*
 * @(#)ListWorkflowScheduleTriggerAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.web;

import java.sql.Connection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.quartz.SchedulerException;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.workflow.WorkflowErrorConstant;
import com.dcivision.workflow.bean.WorkflowGroup;
import com.dcivision.workflow.dao.WorkflowGroupDAObject;
import com.dcivision.workflow.dao.WorkflowRecordDAObject;
import com.dcivision.workflow.dao.WorkflowScheduleTriggersDAObject;

/**
 * <p>Class Name:       ListWorkflowScheduleTriggerAction.java    </p>
 * <p>Description:      The list action class for ListWorkflowScheduleTrigger.jsp</p>
 * @author           Tony
 * @company          DCIVision Limited
 * @creation date    22/12/2004
 * @version          $Revision: 1.5.2.2 $
 */
public class ListWorkflowScheduleTriggerAction extends AbstractListAction {

  public static final String REVISION = "$Revision: 1.5.2.2 $";


  /** Creates a new instance of ListWorkflowScheduleTriggerAction */
  public ListWorkflowScheduleTriggerAction() {
    super();
    this.setListName("workflowScheduleTriggerList");
    this.setListFunctionName("getFullList");
  }

  /** getFunctionCode
   *
   * Abstract function which sub-class should implement to return the corresponding
   * function code.
   *
   * @return   The function code
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.WORKFLOW_SCHEDULE);
  }

  /** getMajorDAOClassName
   *
   * Abstract function which sub-class should implement to return the corresponding
   * major DAO class name used in this class.
   *
   * @return   The DAO class name
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.workflow.dao.WorkflowScheduleTriggersDAObject");
  }

  /**
   * setPageTitle
   * set the extend page title and page path.
   * default page path/title will be created by navmode and functionCode
   */
  public void setPageTitle(HttpServletRequest request, HttpServletResponse response, ActionForm form, ActionMapping mapping, ActionForward actionForward){
    WorkflowGroup currentGroup = (WorkflowGroup)request.getAttribute("workflowGroup");
    String extendTitle = currentGroup.getGroupName();
    request.setAttribute(GlobalConstant.EXTEND_PAGE_TITLE, extendTitle);
    request.setAttribute(GlobalConstant.EXTEND_PAGE_TITLE_SHOW_ONLY, new Boolean(true));
  }

  public void getListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    ListWorkflowScheduleTriggerForm searchForm = (ListWorkflowScheduleTriggerForm) form;
    WorkflowGroupDAObject wfGroupDAO = new WorkflowGroupDAObject(sessionContainer, conn);
    WorkflowRecordDAObject recordDAO = new WorkflowRecordDAObject(sessionContainer, conn);
//    WorkflowTriggersLogDAObject

    WorkflowScheduleTriggersDAObject triggerDAO = new WorkflowScheduleTriggersDAObject(sessionContainer, conn);
    WorkflowGroup wfGroup = (WorkflowGroup) wfGroupDAO.getObjectByID(TextUtility.parseIntegerObj(searchForm.getWorkflowGroupID()));
    request.setAttribute("workflowGroup", wfGroup);

    super.getListData(mapping, form, request, response);
/*
    List resultList = triggerDAO.getFullList();
    List triggerList = new ArrayList();
    for ( int i = 0; i < resultList.size(); i++) {
      WorkflowScheduleTriggers trigger = (WorkflowScheduleTriggers)resultList.get(i);
      //get Workflow Code
      WorkflowRecord record = (WorkflowRecord)recordDAO.getObjectByID(trigger.getWorkflowRecordID());
      trigger.setWorkflowCode(record.getWorkflowCode());

      //get Schedule

      //get Last Fire Time

      //get Next Fire Time

      //get Last Result


    }
*/

  }

  public void deleteListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    super.deleteListData(mapping, form, request, response);
    //Cancel the Schedule Job
    String[] selectedID = form.getBasicSelectedID();

    try {
      log.debug("=== Cancel Schedule_Job ===");
      for (int i = 0; i < selectedID.length; i++) {
        if (!Utility.isEmpty(selectedID[i])) {
          org.quartz.Scheduler sched = com.dcivision.framework.SchedulerFactory.getScheduler();

          log.debug("JOB:"+GlobalConstant.JOB_PREFIX_WORKFLOW_SCHEDULE + selectedID[i] +":" +
            GlobalConstant.JOB_PREFIX_WORKFLOW_SCHEDULE + "GROUP");

          sched.unscheduleJob( GlobalConstant.JOB_PREFIX_WORKFLOW_SCHEDULE + selectedID[i],
            GlobalConstant.JOB_PREFIX_WORKFLOW_SCHEDULE + "GROUP" );
        }
      }
    } catch (SchedulerException se) {
      log.debug("=== Cancel Schedule_Job Fail ===");
      throw new ApplicationException(WorkflowErrorConstant.CANCEL_SCHEDULE_JOB_FAIL);
    }
  }

}
